//
//  BVChooseSpouseViewController.m
//  XiangYu
//
//  Created by YYKJ on 2017/6/20.
//  Copyright © 2017年 YYKJ. All rights reserved.
//  择偶资料修改

#import "BVChooseSpouseViewController.h"

#import "BVMineSingleAlertView.h"
#import "BVAddressAlertView.h"
#import "BVMineTimeAlertView.h"
#import "BVMineProfessionalAlertView.h"
#import "BVTextFieldAlertView.h"
#import "BVMineSpouseDataCell.h"

#import "BVMineModel.h"
#import "BVMineTool.h"

#define BUY_MINE_TABLEVIEWCELL1 @"BVMineSpouseDataCell"

@interface BVChooseSpouseViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic ,strong) UITableView      *tabView;
@property (nonatomic ,strong) NSArray          *titleArr;
@property (nonatomic ,strong) UIView           *bgViewOne;
@property (nonatomic ,strong) UIImageView      *headerView;
@property (nonatomic ,strong) BVMineTool       *mineTool;

@end

@implementation BVChooseSpouseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor BVBackgroundLightGrayColor]];
    
    [self layoutUI];
    [self fetchSpouseConditions];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setSpouseConditions) name:@"setSpouseConditions" object:nil];
}

-(void)layoutUI{
    
    [self.view addSubview:self.tabView];
}

#pragma mark -
#pragma mark - UITableViewDelegate,UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [UIView new];
    [view setBackgroundColor:[UIColor BVBackgroundLightGrayColor]];
    return view;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.titleArr.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    BVMineModel *model = [BVMineModel shareTools];
    
    BVMineSpouseDataCell *cell = [tableView dequeueReusableCellWithIdentifier:BUY_MINE_TABLEVIEWCELL1];
        
    [cell.titleLb setText:[NSString stringWithFormat:@"%@",self.titleArr[indexPath.row][@"titel"]]];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.index = indexPath;
    cell.model = model;
    
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    BVMineModel *model = [BVMineModel shareTools];
    
    switch (indexPath.row) {
        case 1:{
            BVMineSingleAlertView *alertView = [[BVMineSingleAlertView alloc]init:Spouse_height];
            [alertView showMKPAlertView];
            alertView.AlertResult = ^(NSString *str) {
                model.spouse_height = [str mutableCopy];
                [self.tabView reloadData];
            };
            
        }
            break;
        case 2:{
            BVMineSingleAlertView *alertView = [[BVMineSingleAlertView alloc]init:Spouse_education];
            [alertView showMKPAlertView];
            alertView.AlertResult = ^(NSString *str) {
                model.spouse_education = [str mutableCopy];
                [self.tabView reloadData];
            };
        }
            break;
        case 3:{
            BVAddressAlertView *alertView = [[BVAddressAlertView alloc]init];
            [alertView showMKPAlertView];
            alertView.AlertResult = ^(NSString *str) {
                model.spouse_address = [str mutableCopy];
                [self.tabView reloadData];
            };
        }
            break;
        case 5:{
            BVMineSingleAlertView *alertView = [[BVMineSingleAlertView alloc]init:Faith];
            [alertView showMKPAlertView];
            alertView.AlertResult = ^(NSString *str) {
                model.spouse_faith = [str mutableCopy];
                [self.tabView reloadData];
            };
        }
            break;
        case 6:{
            BVMineSingleAlertView *alertView = [[BVMineSingleAlertView alloc]init:Spouse_marriage];
            [alertView showMKPAlertView];
            alertView.AlertResult = ^(NSString *str) {
                model.spouse_maritalStatus = [str mutableCopy];
                [self.tabView reloadData];
            };
        }
            break;
        case 7:{
            BVMineSingleAlertView *alertView = [[BVMineSingleAlertView alloc]init:Spouse_housePurchase];
            [alertView showMKPAlertView];
            alertView.AlertResult = ^(NSString *str) {
                model.spouse_housePurchase = [str mutableCopy];
                [self.tabView reloadData];
            };
        }
            break;
        case 8:{
            BVMineSingleAlertView *alertView = [[BVMineSingleAlertView alloc]init:Spouse_carPurchase];
            [alertView showMKPAlertView];
            alertView.AlertResult = ^(NSString *str) {
                model.spouse_carPurchase = [str mutableCopy];
                [self.tabView reloadData];
            };
        }
            break;
        case 9:{
            BVMineSingleAlertView *alertView = [[BVMineSingleAlertView alloc]init:Spouse_monthlySalary];
            [alertView showMKPAlertView];
            alertView.AlertResult = ^(NSString *str) {
                model.spouse_monthlySalary = [str mutableCopy];
                [self.tabView reloadData];
            };
        }
            break;
        case 10:{
            BVMineSingleAlertView *alertView = [[BVMineSingleAlertView alloc]init:Spouse_liveParents];
            [alertView showMKPAlertView];
            alertView.AlertResult = ^(NSString *str) {
                model.spouse_liveParents = [str mutableCopy];
                [self.tabView reloadData];
            };
        }
            break;
        case 11:{
            BVMineSingleAlertView *alertView = [[BVMineSingleAlertView alloc]init:Spouse_wantChild];
            [alertView showMKPAlertView];
            alertView.AlertResult = ^(NSString *str) {
                model.spouse_wantChild = [str mutableCopy];
                [self.tabView reloadData];
            };
        }
            break;
        default:
            break;
    }
}

-(void)setSpouseConditions{

    __block BVChooseSpouseViewController *  blockSelf = self;
    [self.mineTool setSpouseConditionsSuccess:^(NSString *status) {
        NSLog(@"这里执行吗");
        [blockSelf showSuccess:status];
    } codeFailure:^(NSString *str) {
        [blockSelf showError:str];
    } failure:^(NSError *error) {
        [blockSelf showError:@"网络错误"];
    }];
    
}

-(void)fetchSpouseConditions{
    
    [self.mineTool fetchSpouseConditionsSuccess:^(NSString *status) {
        [self.tabView reloadData];
    } codeFailure:^(NSString *str) {
        [self showMessage:str];
    } failure:^(NSError *error) {
        [self showError:@"网络错误"];
    }];
}

-(UITableView *)tabView{
    if (!_tabView) {
        self.tabView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH-150-64-40) style:UITableViewStylePlain];
        [self.tabView setDelegate:self];
        [self.tabView setDataSource:self];
        [self.tabView setBackgroundColor:[UIColor clearColor]];
        [self.tabView setTableFooterView:[UIView new]];
        [self.tabView setShowsVerticalScrollIndicator:NO];
        [self.tabView setRowHeight:50];
        [self.tabView registerClass:[BVMineSpouseDataCell class] forCellReuseIdentifier:BUY_MINE_TABLEVIEWCELL1];
    }
    return _tabView;
}

-(BVMineTool *)mineTool{
    if (!_mineTool) {
        _mineTool = [[BVMineTool alloc]init];
    }
    return _mineTool;
}

-(NSArray *)titleArr{
    if (!_titleArr) {
        self.titleArr = [NSArray arrayWithObjects:
                           @{@"titel":@"年龄"},
                           @{@"titel":@"身高"},
                           @{@"titel":@"学历"},
                           @{@"titel":@"所在地"},
                           @{@"titel":@"诚信星级"},
                           @{@"titel":@"信仰"},
                           @{@"titel":@"婚姻状况"},
                           @{@"titel":@"购房情况"},
                           @{@"titel":@"购车情况"},
                           @{@"titel":@"月收入"},
                           @{@"titel":@"是否愿意与父母同住"},
                           @{@"titel":@"是否想要小孩"},nil];
    }
    return _titleArr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
